﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wedn.Core.Utility;

namespace Wedn.Web.UI.Admin.Include
{
    public partial class Media : AdminInclude
    {
        /// <summary>
        /// 带有链接的当前路径
        /// </summary>
        protected string currentPath;
        //
        protected DirectoryInfo currentDir;
        protected string path = RequestHelper.QueryString("path", true);
        protected void Page_Load(object sender, EventArgs e)
        {
            path = string.IsNullOrEmpty(path) ? "/upload/" : path;
            currentDir = new DirectoryInfo(Server.MapPath(path));
            currentPath = GetPathMap();
        }
        /// <summary>
        /// 获取路径导航HTML
        /// </summary>
        /// <returns></returns>
        private string GetPathMap()
        {
            if (!string.IsNullOrEmpty(path))
            {
                string pathHtmlFormat = "/<a class=\"dir\" href=\"?path=/{0}/\">{1}</a>";
                StringBuilder result = new StringBuilder();
                string[] dirs = path.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                List<string> temp = new List<string>();
                for (int i = 0; i < dirs.Length; i++)
                {
                    temp.Add(dirs[i]);
                    result.AppendFormat(pathHtmlFormat, string.Join("/", temp), dirs[i]);
                }
                return result.ToString();
            }
            //缺省情况
            return "/<a href=\"?path=/upload/\">upload</a>";
        }

        /// <summary>
        /// 获取文件对应的图标
        /// </summary>
        /// <param name="fileExtension"></param>
        /// <returns></returns>
        protected string GetFileImage(string fileExtension)
        {
            switch (fileExtension.ToLower())
            {
                case ".gif":
                case ".jpg":
                case ".png":
                case ".bmp":
                case ".tif":
                    return "image.png";

                case ".docx":
                    return "docx.png";

                case ".doc":
                case ".rtf":
                    return "doc.png";

                case ".ppt":
                    return "ppt.png";

                case ".pptx":
                    return "pptx.png";

                case ".xls":
                case ".csv":
                    return "xls.png";

                case ".xlsx":
                    return "xlsx.png";

                case ".pdf":
                    return "pdf.png";

                case ".rar":
                case ".zip":
                case ".cab":
                case ".7z":
                    return "rar.png";

                case ".wav":
                case ".wmv":
                case ".mpeg":
                case ".avi":
                    return "media.png";
                case ".wma":
                case ".mp3":
                    return "music.png";

                case ".ini":
                case ".txt":
                case ".css":
                case ".js":
                case ".htm":
                case ".html":
                case ".xml":
                case ".h":
                case ".c":
                case ".php":
                case ".vb":
                case ".cpp":
                case ".cs":
                case ".aspx":
                case ".asm":
                case ".sln":
                case ".vs":
                    return "txt.png";

                case ".fla":
                case ".flv":
                    return "fla.png";

                case ".swf":
                    return "swf.png";

                case ".psd":
                    return "psd.png";

                case ".ai":
                    return "ai.png";

                case ".chm":
                    return "document.png";

                case ".dll":
                case ".exe":
                case ".msi":
                case ".db":
                    return "exe.png";

                default: return "default.gif";
            }
        }
    }
}